package com.leyou.item.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.service.BrandService;
import com.leyou.item.service.CategoryBrandService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("brand")
public class BrandController {

    private final BrandService brandService;

    private final CategoryBrandService categoryBrandService;

    public BrandController(BrandService brandService, CategoryBrandService categoryBrandService) {
        this.brandService = brandService;
        this.categoryBrandService = categoryBrandService;
    }

    /**
     * 根据ID查询品牌
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> findBrandById(@PathVariable("id") long id) {
        return ResponseEntity.ok(this.brandService.findBrandById(id));
    }

    /**
     * 根据品牌Id集合查询品牌集合
     *
     * @param ids
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> listBrandById(@RequestParam("ids") List<Long> ids) {
        return ResponseEntity.ok(this.brandService.listBrandById(ids));
    }


    /**
     * 分页查询品牌
     *
     * @param page
     * @param rows
     * @param key
     * @return
     */
    @GetMapping("/page")
    public ResponseEntity<PageDTO<BrandDTO>> pageQuery(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "key", required = false) String key
    ) {
        return ResponseEntity.ok(this.brandService.pageQuery(page, rows, key));
    }

    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> listBrandCategory(
            @RequestParam("id") Long cid
    ) {
        return ResponseEntity.ok(this.brandService.listBrandCategory(cid));
    }

    /**
     * 新增品牌
     * @param brandDTO
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> addBrand(BrandDTO brandDTO) {
        this.brandService.addBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 更新品牌
     * @param brandDTO
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateBrand(BrandDTO brandDTO) {
        this.brandService.updateBrand(brandDTO);
        return ResponseEntity.ok().build();
    }

    /**
     * 根据ID删除品牌
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteBrandById(@PathVariable("id") Long id){
        // 删除品牌表
        brandService.removeById(id);
        // TODO 删除中间表
        categoryBrandService.remove(
                new QueryWrapper<CategoryBrand>().eq("brand_id", id));
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}